const userDb = require('../db/user-db')
const {logw} = require('../log')

const checkDeviceNo = function (deviceInfo) {
    return new Promise((resolve, reject) => {
        const maddr = deviceInfo.maddr;
        const deviceNo = deviceInfo.deviceNo;
        const deviceInfoBody={};
        userDb.deviceInfo(maddr)
            .then((result) => {
                if(!result){
                    deviceInfoBody.code = 10011;
                    deviceInfoBody.body = 'Invalid deviceNo';
                    resolve(deviceInfoBody);
                }else{
                    let isLogin = 0;
                    for(let i=0;i<result.length;i++){
                        if(result[i].deviceNo === deviceNo){
                            isLogin = 1;
                            break;
                        }
                    }
                    if(isLogin === 0){
                        deviceInfoBody.code = 10011;
                        deviceInfoBody.body = 'Invalid deviceNo';
                        resolve(deviceInfoBody);
                    }else{
                        deviceInfoBody.code = 0;
                        resolve(deviceInfoBody);
                    }
                }
            })
            .catch((err) => {
                deviceInfoBody.code = 10001;
                deviceInfoBody.body = err;
                resolve(deviceInfoBody);
            })
    });


};

module.exports = checkDeviceNo;

